$(function () {
    $('#form').bootstrapValidator({

        excluded: [':disabled', ':hidden', ':not(:visible)'],

        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok-sign',
            invalid: 'glyphicon glyphicon-remove-circle',
            validating: 'glyphicon glyphicon-refresh'
        },


        fields: {
            username: {
                validators: {
                    notEmpty: {
                        message: "用户名不能为空"
                    },
                    stringLength: {
                        min: 2,
                        max: 6,
                        message: "用户名在2-6位"
                    },
                    callback:{
                        message:'用户名不存在'
                    }
                }
            },
            password: {
                validators: {
                    notEmpty: {
                        message: "密码不能为空"
                    },
                    stringLength: {
                        min: 6,
                        max: 12,
                        message: '密码在6-12位'
                    },
                    callback:{
                        message:'密码不正确'
                    }
                }
            }
        }


    })

    // 重置表单
    $('.btn-ret').click(function () {

        $('#form').data("bootstrapValidator").resetForm();
    })

    // 登入请求
    $('#form').on('success.form.bv', function (e) {
        e.preventDefault()
        // console.log($('#form').serialize());


        $.ajax({
            url: '/employee/employeeLogin',
            type: 'post',
            data: $('#form').serialize(),
            dataType: 'json',
            success: function (info) {
                console.log(info);
                // 判断是否登入成功
                if (info.success) {
                    //登入成功      
                    location.href = 'index.html'
                } else if ( info.error ==1000 ) {
                    //"用户名不存在! "
                    $('#form').data('bootstrapValidator').updateStatus('username', 'INVALID','callback')
                    
                }else {
                    //密码不对
                    $('#form').data('bootstrapValidator').updateStatus('password', 'INVALID','callback')
                }

            }
        })
    })

})
